﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using WindowsInput;
using System.Windows.Threading;
using System.Windows.Interop;

namespace MercuryFE
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        public static NavigationHook __navigationHook = new NavigationHook();
        public static SoundHandler __soundHandler = null;
        

        private static DispatcherTimer _timer = new DispatcherTimer();
        //private static Inputs _inputs = null;

        //private static bool _isVerticalMenu = true;
        private static bool _hasVerticalMenu = true;
        private static bool _hasHorizontalMenu = true;


        private static MyWindow _window = null;
        private static IntPtr _windowHandle = IntPtr.Zero;




        public App()
        {
           // __navigationHook.setup(new System.Windows.Forms.KeyEventHandler(this.HandleNavigation));

            this.Exit += new ExitEventHandler(App_Exit);
        }

        public static void InitialiseNavigation(bool HasVerticalMenu, bool HasHorizontalMenu, MyWindow window)
        {
            _window = window;
            _windowHandle = new WindowInteropHelper(_window).Handle;
            _hasVerticalMenu = HasVerticalMenu;
            _hasHorizontalMenu = HasHorizontalMenu;
            //_inputs = new Inputs(windowHandle, true, true, true); // checks for all devices...
            __navigationHook.InitialiseInputs(_windowHandle); //, true, false, false);
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 10);  // 16ms?  There are 10,000 ticks in a millisecond
            _timer.Tick += new EventHandler( _timer_Tick);
            _timer.Start();

        }

        /// <summary>
        /// Stops the polling of input devices used for navigation
        /// </summary>
        public static void PauseNavigation()
        {
            _timer.Stop();
        }

        /// <summary>
        /// Resumes polling of input devices used for navigation
        /// </summary>
        public static void ResumeNavigation()
        {
            _timer.Start();
        }

        /// <summary>
        /// This method actually polls all the input devices to determine what navigation needs to be done.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void _timer_Tick(object sender, EventArgs e)
        {
            NavigationHook.NavigationTask naviTask = NavigationHook.NavigationTask.None;
            bool normalSpeed = true;
            __navigationHook.PollDevices(_hasVerticalMenu, _hasHorizontalMenu, ref naviTask, ref normalSpeed );

            if(naviTask != NavigationHook.NavigationTask.None)
                _window.DoNavigation(naviTask, normalSpeed);
                //getActiveWindow().DoNavigation(naviTask, normalSpeed);
        }



        void App_Exit(object sender, ExitEventArgs e)
        {
            if(__soundHandler != null)
                __soundHandler.saveTracksToDBQueue(true);
        }


        //private static MyWindow getActiveWindow()
        //{
        //    MyWindow activeWin = null;
        //    foreach (MyWindow win in App.Current.Windows)
        //    {
        //        if (win.IsActive)
        //        {
        //            activeWin = win;
        //            break;
        //        }
        //    }

        //    return activeWin;
        //}
/*
        public void HandleNavigation(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            bool isNavigate = true;
            bool doSuppress = false;
            string newKeyCode = App.__navigationHook.getnewKeyCode(e.KeyCode, ref isNavigate, ref doSuppress);

            if (doSuppress)
            {
                e.SuppressKeyPress = true;
                e.Handled = true;

            }

            if (newKeyCode != null)
            {
                if (isNavigate)
                {
                    NavigationHook.NavigationTask naviTask = (NavigationHook.NavigationTask)Enum.Parse(typeof(NavigationHook.NavigationTask), newKeyCode);


                    //// get active window

                    //MyWindow activeWin = null;
                    //foreach (MyWindow win in App.Current.Windows)
                    //{
                    //    if (win.IsActive)
                    //    {
                    //        activeWin = win;
                    //        break;
                    //    }
                    //}

                    //activeWin.DoNavigation(naviTask, true);
                    _window.DoNavigation(naviTask, true);
                }
                else
                {

                    //WindowCollection wins = Application.Current.Windows;

                    VirtualKeyCode keyCode = (VirtualKeyCode)Enum.Parse(typeof(VirtualKeyCode), newKeyCode);

                    if (__navigationHook.ProcessWindowHandle != IntPtr.Zero)
                        ClientLib.SetForegroundWindow(__navigationHook.ProcessWindowHandle);
                    WindowsInput.InputSimulator.SimulateKeyDown(keyCode);
                    WindowsInput.InputSimulator.SimulateKeyUp(keyCode);

                }
            }


        }
 * */
        //public void HandleNavigation(object sender, System.Windows.Forms.KeyEventArgs e)
        //{
        //    bool isNavigate = true;
        //    bool doSuppress = false;
        //    string newKeyCode = App.__navigationHook.getnewKeyCode(e.KeyCode, ref isNavigate, ref doSuppress);

        //    if (doSuppress)
        //    {
        //        e.SuppressKeyPress = true;
        //        e.Handled = true;

        //    }

        //    if (newKeyCode != null)
        //    {
        //        if (isNavigate)
        //        {
        //            NavigationHook.NavigationTask naviTask = (NavigationHook.NavigationTask)Enum.Parse(typeof(NavigationHook.NavigationTask), newKeyCode);


        //            // get active window

        //            MyWindow activeWin = null;
        //            foreach (MyWindow win in App.Current.Windows)
        //            {
        //                if (win.IsActive)
        //                {
        //                    activeWin = win;
        //                    break;
        //                }
        //            }

        //            activeWin.DoNavigation(naviTask);
        //        }
        //        else
        //        {

        //            //WindowCollection wins = Application.Current.Windows;

        //            VirtualKeyCode keyCode = (VirtualKeyCode)Enum.Parse(typeof(VirtualKeyCode), newKeyCode);

        //            if (__navigationHook.ProcessWindowHandle != IntPtr.Zero)
        //                ClientLib.SetForegroundWindow(__navigationHook.ProcessWindowHandle);
        //            WindowsInput.InputSimulator.SimulateKeyDown(keyCode);
        //            WindowsInput.InputSimulator.SimulateKeyUp(keyCode);
                   
        //        }
        //    }


        //}

    }
}
